package com.woniuxy.ticketuser.rabbitMQ3;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @ClassName Demo
 * @Description
 * @Author zx
 * @Date 2021-03-10 11:18:12
 * @Version 1.0
 */
public class RabbitMQSend_Route {
    private final static String QUEUE_NAME = "hello_publish";
    private final static String QUEUE_NAME_2 = "hello_publish_2";
    private final static String QUEUE_NAME_3 = "hello_publish_3";
    private static final String EXCHANGE_NAME = "ex1";
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            //交换机的类型type为direct是精确匹配，topic是模糊匹配，
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");

            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"one");

            channel.queueDeclare(QUEUE_NAME_2, false, false, false, null);
            channel.queueBind(QUEUE_NAME_2,EXCHANGE_NAME,"one");

            channel.queueDeclare(QUEUE_NAME_3, false, false, false, null);
            channel.queueBind(QUEUE_NAME_3,EXCHANGE_NAME,"one");

            String message = "发给所有";
            //第二个参数给哪个交换机发消息为one的还是为two的
            channel.basicPublish(EXCHANGE_NAME,"one", null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
